package com.tanglon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanglon.constants.AdminConstants;
import com.tanglon.constants.SystemConstants;
import com.tanglon.domain.ResponseResult;
import com.tanglon.domain.dto.AddRoleDTO;
import com.tanglon.domain.dto.RoleStatusDTO;
import com.tanglon.domain.dto.UpdateRoleDTO;
import com.tanglon.domain.entity.Role;
import com.tanglon.domain.entity.RoleMenu;
import com.tanglon.domain.vo.PageVo;
import com.tanglon.domain.vo.RoleVo;
import com.tanglon.mapper.RoleMapper;
import com.tanglon.service.RoleMenuService;
import com.tanglon.service.RoleService;
import com.tanglon.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 角色信息表(Role)表服务实现类
 *
 * @author makejava
 * @since 2025-08-13 11:23:27
 */
@Service("roleService")
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMenuService roleMenuService;

    @Autowired
    private RoleService roleService;

    @Override
    public List<String> selectRoleKeyByUserId(Long id) {
        //是否为超级管理员
        if(id == 1L || id == 14787164048663L){
            List<String> roleKeys = new ArrayList<>();
            roleKeys.add("admin");
            return roleKeys;
        }
        //否则查询用户所具有的角色信息
        return getBaseMapper().selectRoleKeyByUserId(id);

    }

    @Override
    public ResponseResult roleList(Integer pageNum, Integer pageSize, String roleName, String status) {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Objects.nonNull(roleName) ,Role::getRoleName, roleName);
        wrapper.eq(Objects.nonNull(status) ,Role::getStatus, status);
        //升序根据role_sort
        wrapper.orderByAsc(Role::getRoleSort);
        Page<Role> page = new Page<>(pageNum, pageSize);
        page(page,wrapper);

        PageVo pageVo = new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(page.getRecords());

        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult changeStatus(RoleStatusDTO roleStatusDTO) {
        LambdaUpdateWrapper<Role> queryWrapper = new LambdaUpdateWrapper<>();
        queryWrapper.eq(Role::getId,roleStatusDTO.getRoleId());
        queryWrapper.set(Role::getStatus,roleStatusDTO.getStatus());
        update(queryWrapper);
        return ResponseResult.okResult();
    }

    @Override
    @Transactional
    public ResponseResult addRole(AddRoleDTO addRoleDTO) {
        // 添加角色权限
        Role role = BeanCopyUtils.copyBean(addRoleDTO, Role.class);
        save(role);
        Long roleId = role.getId();

        List<RoleMenu> roleMenuList = addRoleDTO.getMenuIds().stream()
                .map(menuId -> new RoleMenu(roleId, menuId))
                .collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenuList);
        return ResponseResult.okResult();
    }


    @Override
    public ResponseResult roleDetail(Integer id) {
        Role role = getById(id);
        RoleVo roleVo = BeanCopyUtils.copyBean(role, RoleVo.class);
        return ResponseResult.okResult(roleVo);
    }

    @Override
    @Transactional
    public ResponseResult updateRole(UpdateRoleDTO updateRoleDTO) {
        Role role = BeanCopyUtils.copyBean(updateRoleDTO, Role.class);
        updateById(role);
        //先删再加
        LambdaQueryWrapper<RoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RoleMenu::getRoleId,role.getId());
        roleMenuService.remove(queryWrapper);

        List<RoleMenu> roleMenuList = updateRoleDTO.getMenuIds().stream()
                .map(menuId -> new RoleMenu(role.getId(), menuId))
                .collect(Collectors.toList());
        roleMenuService.saveBatch(roleMenuList);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteRole(Integer id) {
        roleService.removeById(id);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult roleListAll() {
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Role::getStatus, AdminConstants.STATUS_NORMAL);
        List<Role> list = list();
        return ResponseResult.okResult(list);
    }


}

